/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/9  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.AmtAccount;
import com.sansi.pencilbook.mapper.AmtAccountMapper;
import com.sansi.pencilbook.service.AmtAccountServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能说明: 资金账户服务
 *
 * @author lisy31662
 */
@Service
public class AmtAccountServerImpl implements AmtAccountServer {

  @Autowired
  private AmtAccountMapper amtAccountMapper;

  @Override
  public Result getAmtAccountALL() {
    try {
      List<AmtAccount> list = amtAccountMapper.findAmtAccountAll();
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed("查询失败");
    }
  }

  @Override
  public Result getAmtAccountByUser(int userId) {
    try {
      AmtAccount amtAccount = amtAccountMapper.findAmtAccountByUser(userId);
      return amtAccount!=null ? Result.ofSuccess("查询成功", 1, amtAccount) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed("查询失败");
    }
  }

  @Override
  public Result getAmtAccountByAccount(int accountId) {
    try {
      AmtAccount amtAccount = amtAccountMapper.findAmtAccountByAccout(accountId);
      return amtAccount!=null ? Result.ofSuccess("查询成功", 1, amtAccount) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed("查询失败");
    }
  }

  @Override
  public Result modifyAmtAccount(AmtAccount amtAccount) {
    try {
      int num = amtAccountMapper.updateAmtAccount(amtAccount);
      return num>0 ? Result.ofSuccess("修改成功", 1) :
          Result.ofFailed("修改失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result modifyAmtAccount(int userId, float amt) {
    try {
      AmtAccount amtAccount = new AmtAccount();
      amtAccount.setUserId(userId);
      amtAccount.setAmt(amt);
      int num = amtAccountMapper.updateAmtAccount(amtAccount);
      return num>0 ? Result.ofSuccess("修改成功", 1) :
          Result.ofFailed("修改失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result addAmtAccount(int userId) {
    try {
      AmtAccount amtAccount = new AmtAccount();
      amtAccount.setUserId(userId);
      amtAccount.setAmt(0);
      int num = amtAccountMapper.updateAmtAccount(amtAccount);
      return num>0 ? Result.ofSuccess("新增成功", 1) :
          Result.ofFailed("新增失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result purchase(int userId, float amt) {
    try {
      AmtAccount amtAccount = amtAccountMapper.findAmtAccountByUser(userId);
      amtAccount.setAmt(amtAccount.getAmt()-amt);
      int num = amtAccountMapper.updateAmtAccount(amtAccount);
      return num>0 ? Result.ofSuccess("消费成功", 1) :
          Result.ofFailed("消费失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result recharge(int userId, float amt) {
    try {
      AmtAccount amtAccount = amtAccountMapper.findAmtAccountByUser(userId);
      amtAccount.setAmt(amtAccount.getAmt()+amt);
      int num = amtAccountMapper.updateAmtAccount(amtAccount);
      return num>0 ? Result.ofSuccess("充值成功", 1) :
          Result.ofFailed("充值失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }
}
